package com.freshcity.component.container
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;

	public class Container extends Component
	{
		private var _children:Vector.<DisplayObject>=new Vector.<DisplayObject>();
		protected var childrenChanged:Boolean=false;
		public var hasBack:Boolean;

		public function Container(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
		}

		public function clear():void
		{
			while (numChildren > 0)
			{
				removeChildAt(0);
			}
		}

		override public function addChild(child:DisplayObject):DisplayObject
		{
			_children.push(child);
			return child;
		}

		public function get children():Vector.<DisplayObject>
		{
			return _children;
		}

		/**
		 * Override of addChild to force layout;
		 */
		public function $addChild(child:DisplayObject):DisplayObject
		{
			super.addChild(child);
			return child;
		}

		/**
		 * Override of addChildAt to force layout;
		 */
		public function $addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			super.addChildAt(child, index);
			return child;
		}

		/**
		 * Override of removeChild to force layout;
		 */
		public function $removeChild(child:DisplayObject):DisplayObject
		{
			super.removeChild(child);
			return child;
		}

		/**
		 * Override of removeChild to force layout;
		 */
		public function $removeChildAt(index:int):DisplayObject
		{
			var child:DisplayObject=super.removeChildAt(index);
			return child;
		}

		/**
		 * Internal handler for resize event of any attached component. Will redo the layout based on new size.
		 */
		protected function onResize(event:Event):void
		{
			invalidate();
		}

		public function initialize():void
		{
			while (numChildren > 0)
			{
				$removeChildAt(0);
			}
			var child:DisplayObject
			for each (child in _children)
			{
				$addChild(child)
			}
			//dispatchEvent(new Event(Event.RESIZE));
			draw();
		}

		override public function draw():void
		{
			super.draw();
			if (this.hasBack)
			{
				this.graphics.clear();
				this.graphics.lineStyle(1, 0, 1);
				//beginFill(0x102526);
				this.graphics.drawRoundRect(-5, -5, _width+10, _height+10, 8, 8);
					//drawRect(0, 0, _width, _height);
					//endFill();
			}
		}

	}
}
